<?php
/**
 * Template attributes model. Attributes could assigned to templates (othe words grouped into attribute sets)
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @package Products Module
 * @license  New BSD License
 *
 */
class Models_ProductTemplates_Attributes extends Bel_Model_Abstract  {
	
	/**
	 * Array of tables' classes which are in one-to-many relation with product templates attributes table.  
	 *
	 * @var array
	 */	
	protected $_dependentTables = array('Models_Products_AttributeValues');
	
	/**
	 * Array of many-to-one references with product templates & product attributes.
	 * Relation between product templates and product attributes is many-to-many
	 *
	 * @var array
	 */	
	protected $_referenceMap = array (
								 'Template' => 
										array ('columns' => 'product_template_id',
											   'refTableClass' => 'Models_ProductTemplates',
								   			   'refColumns' => 'product_template_id', 
											   'onDelete' => self::CASCADE
								   			   ),
								 'Attribute' => 
										array ('columns' => 'attribute_id',
											   'refTableClass' => 'Models_ProductAttributes',
								   			   'refColumns' => 'attribute_id',
											   'onDelete' => self::CASCADE 
								   			   )
								  );	
	
    /**
     * Template ID. Used in _selectQuery method. Could be used
     * to store template id to be able to use it further in this class
     * methods
     *
     * @var integer
     */
	private $_template_id = null;
								  
								  
	/**
	 * Constructor. Sets table name & primary key
	 *
	 * @param array $array - see Zend_Db_Table_Abstract for details
	 */
	public function __construct($array = array()) {
		$this->_name = TABLE_PREFIX . 'product_attribute2template';
		$this->_primary = array ('template_attribute_id' );
		parent::__construct ( $array );
	
	}	
	
	/**
	 * Set $_template_id
	 *
	 * @param integer $template_id
	 */
	public function setTemplate($template_id){
		$this->_template_id = $template_id;
	}

	/**
	 * Prepares select query for getAll() method to select product attributes assigned to product template.
	 * Product templae is set via setTemplate
	 *
	 * @return Zend_Db_Select
	 */
	protected function _selectQuery(){
		$select = parent::_selectQuery();
		$oAttr = new Models_ProductAttributes();
		$select->joinInner($oAttr->info('name'),$oAttr->info('name').'.attribute_id='.$this->_name.'.attribute_id')->order('attribute_order');
		
		if($this->_template_id){
			$select->where('product_template_id=\''.$this->_template_id.'\'');
		}
		return $select;
	}
	
}
